package org.albino.evinreisi.service;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;

import org.albino.evinreisi.beans.Post;
import org.albino.evinreisi.dao.PostDao;
import org.albino.evinreisi.facebook.FacebookService;
import org.albino.evinreisi.facebook.FacebookServiceImpl;
import org.albino.evinreisi.facebook.FacebookSession;
import org.albino.evinreisi.facebook.FacebookSession.State;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

@Named("postService")
@Scope("session")
public class PostService {

	@Autowired
	FacebookSession facebookSession;

	PostDao postDao;

	@PostConstruct
	public void init() {
		postDao = new PostDao();
	}

	public List<Post> getPosts() {

		System.out.println(facebookSession);
		
		if (facebookSession != null && facebookSession.getToken() != null) {
			System.out.println("TOKEN TAMAM!");

			try {
				FacebookService facebookService = new FacebookServiceImpl(facebookSession.getToken());

				System.out.println(facebookService.getSelf().getName());
			} catch (Exception e) {
				e.printStackTrace();
				facebookSession.setState(State.DISCONNECTED);
				facebookSession.setToken(null);
			}

		}

		return postDao.getPosts();
	}
}
